package com.java.mall_imooc.controller;

import com.java.mall_imooc.common.enums.ResultEnum;
import com.java.mall_imooc.common.utils.JwtUserInfo;
import com.java.mall_imooc.common.utils.PageUtils;
import com.java.mall_imooc.common.utils.R;
import com.java.mall_imooc.config.JwtProperties;
import com.java.mall_imooc.entity.User;
import com.java.mall_imooc.form.UserLoginForm;
import com.java.mall_imooc.form.UserRegisterForm;
import com.java.mall_imooc.interceoter.LoginInterceptor;
import com.java.mall_imooc.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Arrays;


/**
 * 用户表
 *
 * @author jl
 * @since 2020-03-14 23:41:10
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {
    private UserService userService;
    private JwtProperties jwtProperties;

    /**
     * 注册用户
     */
    @PostMapping("/register")
    public R register(@RequestBody @Valid UserRegisterForm userRegisterForm) {
        return userService.register(userRegisterForm);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login(@RequestBody @Valid UserLoginForm userLoginForm) {
       return userService.login(userLoginForm.getUsername(), userLoginForm.getPassword());
    }

    /**
     * 登出
     * 后台登录退出功能，注销cookie的实现方法，新建一个同样key的cookies，value为null，age为0，相同path
     */
    @PostMapping("/logout")
    public R logout(HttpServletResponse response) {
        Cookie cookie = new Cookie(jwtProperties.getCookieName(),null);
        // Cookie并不能根本意义上删除，只需要这样设置为0即可
        cookie.setMaxAge(0);
        // 设置成跟写入cookies一样的，全路径共享Cookie
        cookie.setPath("/");
        // 重新响应
        response.addCookie(cookie);
        return R.ok();
    }

    /**
     * 获取用户
     */
    @GetMapping
    public R<User> userInfo(HttpSession session) {
        JwtUserInfo userInfo = LoginInterceptor.getUserInfo();
        if (userInfo.getUserId() == null) {
            return R.error(ResultEnum.NEED_LOGIN);
        }

        return R.ok(userService.getById(userInfo.getUserId()));
    }

    /**
     * 带条件分页查询user
     */
    @GetMapping("/list")
    public R<PageUtils> list(User user, @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (pageNum < 1) pageNum = 1;
        if (pageSize > 100) pageSize = 100;
        PageUtils page = userService.queryPage(user, pageNum, pageSize);

        return R.ok(page);
    }

    /**
     * 根据主键查询user
     */
    @GetMapping("/info/{id}")
    public R<User> info(@PathVariable("id") Integer id) {
        User user = userService.getById(id);

        return R.ok(user);
    }

    /**
     * 修改user
     */
    @PutMapping("/update")
    public R update(@RequestBody User user) {
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除user
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
